﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using TKFA.BLL;

namespace FixedAssets.Module.StatisticFAI
{
    public partial class FA_StatisticQuery : System.Web.UI.Page
    {
        FA_PROPERTY property_bll = new FA_PROPERTY();
        FA_MASTERDATA masterdata_bll = new FA_MASTERDATA();
        static DataTable cmdt = new DataTable();
        static List<string> showcolumns = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAll();
            }
        }
        private void LoadAll()
        {
            DataTable mdt=new DataTable ();
          DataTable pdt= property_bll.GetDistinctList();
          DataSet ds=masterdata_bll.GetAllList();
            if(ds.Tables.Count>0)
             mdt = ds.Tables[0];
            showcolumns.Clear();
          showcolumns.Add("ROWID");
          showcolumns.Add("MD_ID");
          showcolumns.Add("SCORPCODE");
          showcolumns.Add("SASSETSCODE");
          showcolumns.Add("SMASTERCARDCODE");
          this.CheckBoxList1.Items.Clear();
          foreach (DataRow dr in pdt.Rows)
          {
              ListItem li = new ListItem(dr["SNAME"].ToString(), dr["SCODE"].ToString());
              li.Attributes.Add("alt", li.Value);
              this.CheckBoxList1.Items.Add(li);

              if (mdt.Columns.Contains(dr["SCODE"].ToString()))
              {
                  mdt.Columns[dr["SCODE"].ToString()].Caption = dr["SNAME"].ToString();
              }
          }
          mdt.PrimaryKey = new DataColumn[] { mdt.Columns["MD_ID"] };
          cmdt = mdt.Copy();
          cmdt.Columns.Add("ROWID", typeof(int));

          for (int i = 1; i <= cmdt.Rows.Count; i++)
          {
              cmdt.Rows[i - 1]["ROWID"] = i;
          }
          C1GridView_DataBind();

          DataTable tjtb = CreateTable();
          tjtb.Rows.Add(CreateTableRow(tjtb));
          C1GridView2.DataSource = tjtb;
          C1GridView2.DataBind();
        }

        protected void DropDownList_Name_SelectedIndexChanged(object sender,EventArgs e)
        {
            DropDownList ddl = (DropDownList)C1GridView2.Rows[0].FindControl("DropDownList_TJ");
            DropDownList name_ddl = (DropDownList)C1GridView2.Rows[0].FindControl("DropDownList_Name");
            DataRow[] drs= property_bll.GetDistinctList().Select(" sname='" + name_ddl.SelectedValue + "'");
            if (drs != null && drs.Length > 0)
            {
                ddl.SelectedIndex = -1;
                ddl.SelectedValue = null;
                ddl.DataSource = cmdt.DefaultView.ToTable(true, new string[] { drs[0]["scode"].ToString() });
                ddl.DataTextField = drs[0]["scode"].ToString();
                ddl.DataValueField = drs[0]["scode"].ToString();
                ddl.DataBind();
                ddl.Items.Insert(0,"全部");
            }
        }

        protected void DropDownList_TJ_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)C1GridView2.Rows[0].FindControl("DropDownList_TJ");
            DropDownList name_ddl = (DropDownList)C1GridView2.Rows[0].FindControl("DropDownList_Name");
            DataRow[] drs = property_bll.GetDistinctList().Select(" sname='" + name_ddl.SelectedValue + "'");
            if (drs != null && drs.Length > 0)
            {
                ddl.SelectedIndex = -1;
                ddl.SelectedValue = null;
                ddl.DataSource = cmdt.DefaultView.ToTable(true, new string[] { drs[0]["scode"].ToString() });
                ddl.DataTextField = drs[0]["scode"].ToString();
                ddl.DataValueField = drs[0]["scode"].ToString();
                ddl.DataBind();
                ddl.Items.Insert(0, "全部");
            }
        }

        protected void add_onclick(object sender, EventArgs e)
        {
            DataTable TJtb = (DataTable)C1GridView2.DataSource;
            TJtb.Rows.Add(CreateTableRow(TJtb));
            C1GridView2.DataSource = TJtb;
            C1GridView2.DataBind();
        }

        protected void del_onclick(object sender, EventArgs e)
        {
            DataTable TJtb = (DataTable)C1GridView2.DataSource;
            TJtb.Rows.Add(CreateTableRow(TJtb));
            C1GridView2.DataSource = TJtb;
            C1GridView2.DataBind();
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem l in this.CheckBoxList1.Items)
            {
                if (l.Selected)
                {
                    if (!showcolumns.Contains(l.Value))
                    {
                        showcolumns.Add(l.Value);
                        C1.Web.Wijmo.Controls.C1GridView.C1BoundField cbf = new C1.Web.Wijmo.Controls.C1GridView.C1BoundField();
                        cbf.HeaderText = l.Text;
                        cbf.DataField = l.Value;
                        cbf.SortExpression = l.Value;
                        this.C1GridView1.Columns.Add(cbf);
                        DropDownList ddl = (DropDownList)C1GridView2.Rows[0].FindControl("DropDownList_Name");
                        ddl.Items.Add(l.Text);
                    }
                }
                else if (!"ROWID,MD_ID,SCORPCODE,SASSETSCODE,SMASTERCARDCODE".Contains(l.Value))
                {
                    if (showcolumns.Contains(l.Value))
                    {
                        showcolumns.Remove(l.Value);
                        this.C1GridView1.Columns.Remove(this.C1GridView1.Columns.ColumnByName(l.Value));
                        DropDownList ddl = (DropDownList)C1GridView2.Rows[0].FindControl("DropDownList_Name");
                        ddl.Items.Remove(l.Text);
                    }
                }
            }
            C1GridView_DataBind();
        }

        private void C1GridView_DataBind(DataTable dt)
        {
            C1GridView1.DataSource = dt;
            C1GridView1.DataBind();
        }

        private void C1GridView_DataBind()
        {
            DataTable dt=cmdt.DefaultView.ToTable(false,showcolumns.ToArray());
            C1GridView1.DataSource = dt;
            C1GridView1.DataBind();
        }

        protected void C1GridView1_PageIndexChanging(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewPageEventArgs e)
        {
            this.C1GridView1.PageIndex = e.NewPageIndex;
            C1GridView_DataBind();
        }

        protected void C1GridView1_ColumnGrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnGroupedEventArgs e)
        {
            C1GridView_DataBind();
        }

        protected void C1GridView1_ColumnUngrouped(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewColumnUngroupedEventArgs e)
        {
            C1GridView_DataBind();
        }

        protected void C1GridView1_Filtered(object sender, EventArgs e)
        {

        }

        private DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("code",typeof(string));
            dt.Columns.Add("tj", typeof(string));
            dt.Columns.Add("value", typeof(string));
            return dt;
        }

        private DataRow CreateTableRow(DataTable dt)
        {
            DataRow dr = dt.NewRow();
            dr["code"] = "";
            dr["tj"] = "";
            dr["value"] = "";
            return dr;
        }

        protected void C1GridView1_RowDataBound(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowEventArgs e)
        {
            if (e.Row.RowType == C1.Web.Wijmo.Controls.C1GridView.C1GridViewRowType.DataRow)
            {
                //鼠标移动到每项时颜色交替效果
                e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#bfe2ff';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:hand";
            }
        }
    }
}